package com.cg.leetcode;

import org.junit.Test;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * 257.二叉树的所有路径
 *
 * @program: LeetCode->LeetCode_257
 * @author: cg
 * @create: 2022-04-24 09:25
 **/
public class LeetCode_257 {

    @Test
    public void test257() {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(5);

        root.right = new TreeNode(3);
        System.out.println(binaryTreePaths(root));
    }

    /**
     * 给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。
     * <p>
     * 叶子节点 是指没有子节点的节点。
     * <p>
     * 示例 1：
     * 输入：root = [1,2,3,null,5]
     * 输出：["1->2->5","1->3"]
     * <p>
     * 示例 2：
     * 输入：root = [1]
     * 输出：["1"]
     * <p>
     * 提示：
     * 树中节点的数目在范围 [1, 100] 内
     * -100 <= Node.val <= 100
     *
     * @param root
     * @return
     */
    public List<String> binaryTreePaths(TreeNode root) {
        // 递归，深度优先搜索
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        List<Integer> paths = new ArrayList<>();
        dfs(root, paths, res);
        return res;
    }

    private void dfs(TreeNode current, List<Integer> paths, List<String> res) {
        paths.add(current.val);
        // 叶子结点
        if (current.left == null && current.right == null) {
            StringBuilder builder = new StringBuilder();
            // 字符串处理
            for (int i = 0; i < paths.size() - 1; i++) {
                builder.append(paths.get(i)).append("->");
            }
            builder.append(paths.get(paths.size() - 1));
            res.add(builder.toString());
            return;
        }
        if (current.left != null) {
            dfs(current.left, paths, res);
            // 回溯
            paths.remove(paths.size() - 1);
        }
        if (current.right != null) {
            dfs(current.right, paths, res);
            // 回溯
            paths.remove(paths.size() - 1);
        }
    }

    /*public List<String> binaryTreePaths(TreeNode root) {
        // 迭代
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        Deque<Object> stack = new ArrayDeque<>();
        // 结点和路径同时入栈
        stack.push(root);
        stack.push(root.val + "");
        while (!stack.isEmpty()) {
            // 结点和路径同时出栈
            String path = (String) stack.pop();
            TreeNode node = (TreeNode) stack.pop();
            // 叶子节点
            if (node.left == null && node.right == null) {
                res.add(path);
            }
            // 右节点
            if (node.right != null) {
                stack.push(node.right);
                stack.push(path + "->" + node.right.val);
            }
            // 左节点
            if (node.left != null) {
                stack.push(node.left);
                stack.push(path + "->" + node.left.val);
            }
        }
        return res;
    }*/

}
